{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Let's start with loading ResNet50 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloading data from https://github.com/fchollet/deep-learning-models/releases/download/v0.2/resnet50_weights_tf_dim_ordering_tf_kernels.h5\n",
      "102858752/102853048 [==============================] - 42s 0us/step\n"
     ]
    }
   ],
   "source": [
    "from keras.applications.resnet50 import ResNet50\n",
    "from keras.preprocessing import image\n",
    "from keras.applications.resnet50 import preprocess_input, decode_predictions\n",
    "import numpy as np\n",
    "\n",
    "resnet_model = ResNet50(weights='imagenet')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Predicted: [('n02100583', 'vizsla', 0.5155441), ('n02092339', 'Weimaraner', 0.2992141), ('n02099849', 'Chesapeake_Bay_retriever', 0.10746128)]\n"
     ]
    }
   ],
   "source": [
    "from keras.preprocessing import image\n",
    "\n",
    "img_path = './images/dog.jpg' \n",
    "\n",
    "img = image.load_img(img_path, target_size=(224, 224))\n",
    "x = image.img_to_array(img)\n",
    "x = np.expand_dims(x, axis=0)\n",
    "x = preprocess_input(x)\n",
    "\n",
    "preds = resnet_model.predict(x)\n",
    "# decode the results into a list of tuples (class, description, probability)\n",
    "# (one such list for each sample in the batch)\n",
    "print('Predicted:', decode_predictions(preds, top=3)[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Let's run through a few test images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "from os import listdir\n",
    "from os.path import isfile, join\n",
    "\n",
    "# Our openCV function that displays the image and it's predicted labels \n",
    "def draw_test(name, preditions, input_im):\n",
    "    \"\"\"Function displays the output of the prediction alongside the orignal image\"\"\"\n",
    "    BLACK = [0,0,0]\n",
    "    expanded_image = cv2.copyMakeBorder(input_im, 0, 0, 0, imageL.shape[1]+300 ,cv2.BORDER_CONSTANT,value=BLACK)\n",
    "    img_width = input_im.shape[1]\n",
    "    for (i,predition) in enumerate(preditions):\n",
    "        string = str(predition[1]) + \" \" + str(predition[2])\n",
    "        cv2.putText(expanded_image,str(name),(img_width + 50,50),cv2.FONT_HERSHEY_COMPLEX_SMALL,2,(0,0,255),1)\n",
    "        cv2.putText(expanded_image,string,(img_width + 50,50+((i+1)*50)),cv2.FONT_HERSHEY_COMPLEX_SMALL,2,(0,255,0),1)\n",
    "    cv2.imshow(name, expanded_image)\n",
    "\n",
    "# Get images located in ./images folder    \n",
    "mypath = \"./images/\"\n",
    "file_names = [f for f in listdir(mypath) if isfile(join(mypath, f))]\n",
    "\n",
    "# Loop through images run them through our classifer\n",
    "for file in file_names:\n",
    "\n",
    "    from keras.preprocessing import image # Need to reload as opencv2 seems to have a conflict\n",
    "    img = image.load_img(mypath+file, target_size=(224, 224))\n",
    "    x = image.img_to_array(img)\n",
    "    x = np.expand_dims(x, axis=0)\n",
    "    x = preprocess_input(x)\n",
    "    \n",
    "    #load image using opencv\n",
    "    img2 = cv2.imread(mypath+file)\n",
    "    imageL = cv2.resize(img2, None, fx=.5, fy=.5, interpolation = cv2.INTER_CUBIC) \n",
    "    \n",
    "    # Get Predictions\n",
    "    preds = resnet_model.predict(x)\n",
    "    preditions = decode_predictions(preds, top=3)[0]\n",
    "    draw_test(\"Predictions\", preditions, imageL) \n",
    "    cv2.waitKey(0)\n",
    "\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Let's now load VGG16 and InceptionV3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import keras\n",
    "import numpy as np\n",
    "from keras.applications import vgg16, inception_v3, resnet50\n",
    " \n",
    "#Loads the VGG16 model\n",
    "vgg_model = vgg16.VGG16(weights='imagenet')\n",
    " \n",
    "# Loads the Inception_V3 model\n",
    "inception_model = inception_v3.InceptionV3(weights='imagenet')\n",
    " \n",
    "# Loads the ResNet50 model \n",
    "# uncomment the line below if you didn't load resnet50 beforehand\n",
    "#resnet_model = resnet50.ResNet50(weights='imagenet')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Compare all 3 Models with the same test images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get images located in ./images folder    \n",
    "mypath = \"./images/\"\n",
    "file_names = [f for f in listdir(mypath) if isfile(join(mypath, f))]\n",
    "\n",
    "# Loop through images run them through our classifer\n",
    "for file in file_names:\n",
    "\n",
    "    from keras.preprocessing import image # Need to reload as opencv2 seems to have a conflict\n",
    "    img = image.load_img(mypath+file, target_size=(224, 224))\n",
    "    x = image.img_to_array(img)\n",
    "    x = np.expand_dims(x, axis=0)\n",
    "    x = preprocess_input(x)\n",
    "    \n",
    "    #load image using opencv\n",
    "    img2 = cv2.imread(mypath+file)\n",
    "    imageL = cv2.resize(img2, None, fx=.5, fy=.5, interpolation = cv2.INTER_CUBIC) \n",
    "    \n",
    "    # Get VGG16 Predictions\n",
    "    preds_vgg_model = vgg_model.predict(x)\n",
    "    preditions_vgg = decode_predictions(preds_vgg_model, top=3)[0]\n",
    "    draw_test(\"VGG16 Predictions\", preditions_vgg, imageL) \n",
    "    \n",
    "    # Get Inception_V3 Predictions\n",
    "    preds_inception = inception_model.predict(x)\n",
    "    preditions_inception = decode_predictions(preds_inception, top=3)[0]\n",
    "    draw_test(\"Inception_V3 Predictions\", preditions_inception, imageL) \n",
    "\n",
    "    # Get ResNet50 Predictions\n",
    "    preds_resnet = resnet_model.predict(x)\n",
    "    preditions_resnet = decode_predictions(preds_resnet, top=3)[0]\n",
    "    draw_test(\"ResNet50 Predictions\", preditions_resnet, imageL) \n",
    "    \n",
    "    cv2.waitKey(0)\n",
    "\n",
    "cv2.destroyAllWindows()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
